package com.chenqi.community.controller;

import com.chenqi.community.entity.Tag;
import com.chenqi.community.entity.Type;
import com.chenqi.community.exception.NotFoundException;
import com.chenqi.community.service.TagService;
import com.chenqi.community.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author Ardai
 */
@Controller
//@RequestMapping("/community")
public class IndexController {

    @Autowired
    private TagService tagService;

    @Autowired
    private TypeService typeService;

    @GetMapping("/")
    public ModelAndView index(ModelAndView modelAndView){
        modelAndView.setViewName("index");
        return modelAndView;
    }
    @GetMapping("/toLogin")
    public ModelAndView toLogin(ModelAndView modelAndView){
        modelAndView.setViewName("login");
        return modelAndView;
    }
    @GetMapping("/toRegister")
    public ModelAndView toRegister(ModelAndView modelAndView){
        modelAndView.setViewName("register");
        return modelAndView;
    }
    @GetMapping("/toPostDetail")
    public ModelAndView toPostDetail(ModelAndView modelAndView) {
        modelAndView.setViewName("postDetail");
        return modelAndView;
    }
    @GetMapping("/toPublic")
    public ModelAndView toPublic(ModelAndView modelAndView) {
        modelAndView.setViewName("public");
        return modelAndView;
    }

    @GetMapping("/toAddPost")
    public ModelAndView toAddPost(ModelAndView modelAndView) {
        modelAndView.setViewName("public");
        List<Tag> tags = tagService.findAllTags();
        modelAndView.addObject("tags",tags);
        List<Type> types = typeService.findAllType();
        modelAndView.addObject("types", types);
        return modelAndView;
    }

    @GetMapping("test")
    public ModelAndView test(ModelAndView modelAndView) {
        throw new NotFoundException("资源未找到");
    }

    @GetMapping("toAdmin")
    public ModelAndView toAdmin(ModelAndView modelAndView) {
        modelAndView.setViewName("/admin/post_list");
        return modelAndView;
    }
}
